package net.entelijan.cobean.examples.showcase.bind.text;

import static org.junit.Assert.assertEquals;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JTextComponentFixture;
import org.junit.Test;

// TODO More Testcases.
public class TextFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(TextFestTestCase.class);

	@Test
	public void testSetNameFromGUI() {
		TextPanel panel = new TextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			TextShowcase sc = new TextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture nameTextBox = frameFix
					.textBox("nameTextField");
			nameTextBox.deleteText();
			nameTextBox.setText("hallo");
			final JTextComponentFixture birthdayTextBox = frameFix
					.textBox("birthdayTextField");
			birthdayTextBox.focus();
			pause(200);
			assertEquals("hallo", sc.getModel().getName());
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetBirthdayFromGUI() {
		TextPanel panel = new TextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			TextShowcase sc = new TextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture nameTextBox = frameFix
					.textBox("nameTextField");
			final JTextComponentFixture birthdayTextBox = frameFix
					.textBox("birthdayTextField");
			birthdayTextBox.deleteText();
			birthdayTextBox.setText("1.1.1999");
			nameTextBox.focus();
			pause(200);
			assertEquals(CobeanTestUtil.createDate("yyyyMMdd", "19990101"), sc
					.getModel().getBirthday());
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetIDFromGUI() {
		TextPanel panel = new TextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			TextShowcase sc = new TextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture nameTextBox = frameFix
					.textBox("nameTextField");
			final JTextComponentFixture idTextBox = frameFix
					.textBox("idTextField");
			idTextBox.deleteText();
			idTextBox.setText("111");
			nameTextBox.focus();
			pause(200);
			assertEquals(111, sc.getModel().getId());
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetNameFromModel() {
		TextPanel panel = new TextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			TextShowcase sc = new TextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture nameTextBox = frameFix
					.textBox("nameTextField");
			sc.getModel().setName("HELLO");
			pause(100);
			nameTextBox.requireText("HELLO");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetBirthdayFromModel() {
		TextPanel panel = new TextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			pause(1000);
			TextShowcase sc = new TextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture birthdayTextBox = frameFix
					.textBox("birthdayTextField");

			sc.getModel().setBirthday(
					CobeanTestUtil.createDate("yyyyMMdd", "20000303"));
			pause(100);
			birthdayTextBox.requireText("03.03.2000");
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testClickUppercase() {
		TextPanel panel = new TextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			pause(1000);
			TextShowcase sc = new TextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JButtonFixture button = frameFix.button("uppercaseButton");
			final JTextComponentFixture nameTextBox = frameFix
					.textBox("nameTextField");

			nameTextBox.requireText("V.S. Naipaul");
			assertEquals("V.S. Naipaul", sc.getModel().getName());

			button.click();
			pause(200);
			nameTextBox.requireText("V.S. NAIPAUL");
			assertEquals("V.S. NAIPAUL", sc.getModel().getName());

		} finally {
			frameFix.cleanUp();
		}

	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
